/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.controller;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Laurent WOUTERS
 */
public class Concat extends Element {
    private xowl.gmi.view.diagram.Container widget;
    private List<Element> children;
    
    public Concat(Controller controller, Element parent, xowl.gmi.model.cvssl.Concat def) {
        super(controller, parent);
        this.children = new ArrayList<>();
        widget = new xowl.gmi.view.diagram.Container(getAlignment(def.getAlignment()), def.getIsPacked());
    }
    
    public void addChild(Element child) {
        children.add(child);
        widget.addChild(child.getWidget());
    }
    
    @Override public xowl.gmi.view.diagram.Widget getWidget() { return widget; }
    
    @Override protected int getContainedASTCount() {
        int count = 0;
        for (Element child : children)
            count += child.getContainedASTCount();
        return count;
    }
    @Override protected int getLeftSiblingsASTCount(Element divider) {
        int count = 0;
        for (Element child : children) {
            if (child == divider)
                break;
            count += child.getContainedASTCount();
        }
        return count + parent.getLeftSiblingsASTCount(this);
    }
    @Override protected boolean insertAST(xowl.gmi.model.ast.ASTNode node) {
        for (Element child : children)
            if (child.insertAST(node))
                return true;
        return false;
    }
    @Override protected ASTRemovalStatus removeAST(xowl.gmi.model.ast.ASTNode node) {
        for (Element child : children) {
            ASTRemovalStatus value = child.removeAST(node);
            if (value != ASTRemovalStatus.NodeNotFound)
                return value;
        }
        return ASTRemovalStatus.NodeNotFound;
    }
    
    @Override public void saveTo(xowl.gmi.model.Diagram diagram) {
        for (Element child : children)
            child.saveTo(diagram);
    }
    @Override public void decommission() {
        for (Element child : children)
            child.decommission();
    }
}
